﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.GoodsCategory
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGoodsCategory();
            }
        }

        protected void BindGoodsCategory()
        {
            Things.BLL.GoodsCategory bll = new BLL.GoodsCategory();
            DataSet ds= bll.GetList(" IsDeleted=0 ");

            ddlGoodsCategory.Items.Clear();
            ddlGoodsCategory.Items.Add(new ListItem(" -- 请选择 -- ", "-1"));

            Things.Common.UICommon.BindDDLTree(ddlGoodsCategory, ds.Tables[0], Guid.Empty, "&nbsp;&nbsp;&nbsp;");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (!PageValidate.IsNumber(txtGoodsCategoryLevel.Text))
            {
                strErr += "商品分类层级格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtGoodsCategoryOrder.Text))
            {
                strErr += "商品分类排序格式错误！\\n";
            }
            Guid parentID = Guid.Empty;
            if (this.ddlGoodsCategory.SelectedValue == "-1")
            {
                parentID = Guid.Empty;
            }
            else
            {
                if (Guid.TryParse(this.ddlGoodsCategory.SelectedValue, out parentID) == false)
                {
                    strErr += "父商品分类有误！\\n";
                }
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string GoodsCategoryName = this.txtGoodsCategoryName.Text;
            int GoodsCategoryLevel = int.Parse(this.txtGoodsCategoryLevel.Text);
            int GoodsCategoryOrder = int.Parse(this.txtGoodsCategoryOrder.Text);
            bool IsDeleted = false;

            Things.Model.GoodsCategory model = new Things.Model.GoodsCategory();
            model.ParentID = parentID;      
            model.GoodsCategoryName = GoodsCategoryName;
            model.GoodsCategoryLevel = GoodsCategoryLevel;
            model.GoodsCategoryOrder = GoodsCategoryOrder;
            model.IsDeleted = IsDeleted;

            Things.BLL.GoodsCategory bll = new Things.BLL.GoodsCategory();
            bll.Add(model);
            Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "add.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
